package org.testis.data.parser;

import org.testis.data.*;
import org.testis.guiappsmodule.data.GuiAppsModuleDataObject;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import java.util.LinkedHashMap;


/**
 * Defines a class to parse data for gui apps module
 * @author nurhat
 */
public class GuiAppsModuleDop implements TestStepDop
{

    /**
     * Parsing data and creating a GuiAppsModuleDataObject
     * Returns null if any required attribute not given
     * @param data
     * @return
     */
    public TestStepDataObject convert(TestStepDataObject data)
    {
        GuiAppsModuleDataObject dataObj =
            new GuiAppsModuleDataObject(data);
        LinkedHashMap<String, String> map = data.getDataToApply();
        String script = map.get(AtofModuleParameterNames.GUI_APP_SCRIPT);
        String params = map.get(AtofModuleParameterNames.GUI_APP_PARAMS);
        if (script != null) {
            if (params == null) {
                params = "";
            }
            dataObj.setScript(script);
            dataObj.setParameters(params);
            String containsString = map.get(
                AtofModuleParameterNames.GUI_APP_CONTAINS_STRING);
            String expectedExitCode = map.get(
                AtofModuleParameterNames.GUI_APP_EXPECTED_EXIT_CODE);

            if (containsString != null) {
                dataObj.setContainsString(containsString);
            }
            if (expectedExitCode != null) {
                dataObj.setExpectedExitCode(expectedExitCode);

            }
            return dataObj;

        }
        else {
            LogSenderService.putMessageOnQueue("GuiAppsModuleDataObjectParser",
                                               AtofLogLevel.ERROR,
                                               "GuiAppsModuleDataObject is not correctly " +
                                               "given." +
                                               AtofModuleParameterNames.GUI_APP_SCRIPT +
                                               " not given.",
                                               data.getMessageId());
        }
        return null;
    }
}
